﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quality.BasicData;
using System.Data.SqlClient;

namespace Quality.BasicData
{
    public partial class assignedindicators : Form
    {
        public assignedindicators()
        {
            InitializeComponent();
            DB.getcombobox(itemcomboBox, "testitems", "num", "itemname");
        }

        private void itemcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            test();
        }
        void test()
        {

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand();

            da.SelectCommand.CommandText = "select * from testindicators where num not in (select indnum from assignedindicators where itemnum='" + itemcomboBox.SelectedValue + "')";

            DataSet dt1 = DB.getlist(da);
            listBox1.DataSource = dt1.Tables[0];
            listBox1.DisplayMember = "indname";
            listBox1.ValueMember = "num";
            listBox1.SelectedIndex = -1;

            da.SelectCommand.CommandText = "select * from testindicators where num  in (select indnum from assignedindicators where itemnum='" + itemcomboBox.SelectedValue + "')";

            DataSet dt2 = DB.getlist(da);
            listBox2.DataSource = dt2.Tables[0];
            listBox2.DisplayMember = "indname";
            listBox2.ValueMember = "num";
            listBox2.SelectedIndex = -1;



        }

        private void input_Click(object sender, EventArgs e)
        {
            SqlCommand comm = new SqlCommand();
            if (listBox1.SelectedIndex != -1 && itemcomboBox.SelectedIndex != -1)
            {

                foreach (DataRowView row in listBox1.SelectedItems)
                {
                    comm.CommandText = "insert into assignedindicators(indnum,itemnum)values(@indnum,@itemnum)";
                    comm.Parameters.Clear();
                    comm.Parameters.AddWithValue("@itemnum", itemcomboBox.SelectedValue);
                    comm.Parameters.AddWithValue("@indnum", row["num"]);

                    DB.dosql(comm);


                }
                test();

            }
        }

        private void output_Click(object sender, EventArgs e)
        {
            SqlCommand comm = new SqlCommand();
            if (listBox2.SelectedIndex != -1 && itemcomboBox.SelectedIndex != -1)
            {

                foreach (DataRowView row in listBox2.SelectedItems)
                {
                    comm.CommandText = "delete assignedindicators where indnum=@indnum and itemnum=@itemnum";
                    comm.Parameters.Clear();
                    comm.Parameters.AddWithValue("@itemnum", itemcomboBox.SelectedValue);
                    comm.Parameters.AddWithValue("@indnum", row["num"]);

                    DB.dosql(comm);


                }
                test();

            }
        }
    }
}
